package com.lry.crawlers.listener;

import com.jnewbie.JHtml;
import com.lry.crawlers.config.service.ConfigService;
import com.lry.crawlers.configuration.RabbitMQConfig;
import com.lry.crawlers.video.entity.VideoVariable;
import com.lry.crawlers.video.pipeline.VideoPipeline;
import com.lry.crawlers.video.processor.VideoPlusJProcessor;
import com.lry.crawlers.video.service.VideoService;
import com.lry.crawlers.video.service.VideoVariableService;
import com.lry.crawlers.information.entity.Information;
import com.lry.crawlers.information.service.InformationService;
import com.lry.crawlers.message.service.MessageService;
import com.lry.crawlers.util.ProcessVariable;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

@Component
public class VideoMQListener {
    @Autowired
    VideoPipeline videoPipeline;
    @Autowired
    VideoVariableService videoVariableService;
    @Autowired
    VideoService videoService;
    @Autowired
    InformationService informationService;
    @Autowired
    MessageService messageService;
    @Autowired
    ConfigService configService;



    //监听video_queue队列
    @RabbitListener(queues = RabbitMQConfig.VIDEOQUEUE_NAME)
    public void ListenerVideoQueue(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String name = new String(message.getBody());

        String[] split = name.split(",");
        if(informationService.getById(split[1])==null){
            channel.basicAck(deliveryTag, false);
            return;
        }
        Thread.sleep(3000);
        //MQ应答
        channel.basicAck(deliveryTag, false);

        informationService.updateInformation(new Information(split[1],"视频","采集关键词："+split[0],"进行中",0,0,new Date(),null,20));

        try {
            videoVariableService.update();

            //搜索内容
            for (VideoVariable f : ProcessVariable.vVariable.values()) {
                //创建请求客户端
                JHtml jHtml = new JHtml();
                jHtml.setEncoding(f.getEncoding());
                //创建数据处理器
                VideoPlusJProcessor.initializeFilter();
                VideoPlusJProcessor videoJProcessor = new VideoPlusJProcessor(split[1],f, videoService,videoPipeline);
                //将客户端加入到处理器,设置开始url,设置启动线程
                videoJProcessor.setJHtml(jHtml)
                        .setGetMethod(f.getMode())
                        .setUrl(f.getSearchUrl().replace("{searchKey}",split[0]))
                        .start(configService.getOne(null).getVideoThread());
            }
            VideoPlusJProcessor.allUrl = 0;
            informationService.updateInformation(new Information(split[1],new Date(),"完成",100));

        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw,true));
            String infomsg = sw.toString();
            channel.basicNack(deliveryTag, false, false);
            informationService.updateInformation(new Information(split[1],new Date(),"错误",10));
            messageService.setMessage(new com.lry.crawlers.message.entity.Message("视频采集出现错误！title："+split[0]+"异常："+infomsg,new Date()));
        }}


}







